﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Sugata.SolutionFoldersOrganizer
{
    public static class Common
    {

        /// <summary>
        /// This functions prints on the debug ouput and on the generic pane of the output window
        /// a text.
        /// </summary>
        /// <param name="text">text to send to Output Window.</param>
        public static void OutputString(string text)
        {

            Trace.WriteLine("Output: " + text);

            // Now print the string on the output window.
            // The first step is to get a reference to IVsOutputWindow.
            var outputWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;

            // If we fail to get it we can exit now.
            if (null == outputWindow)
            {
                Trace.WriteLine("Failed to get a reference to IVsOutputWindow");
                return;
            }

            // Now get the window pane for the general output.
            var guidGeneral = Microsoft.VisualStudio.VSConstants.GUID_OutWindowGeneralPane;
            IVsOutputWindowPane windowPane;

            // following instructions on MEF Output Window forum. if this doesn't work, use the commented out stuff below this
            if (Microsoft.VisualStudio.ErrorHandler.Failed(outputWindow.GetPane(ref guidGeneral, out windowPane)) && (Microsoft.VisualStudio.ErrorHandler.Succeeded(outputWindow.CreatePane(ref guidGeneral, null, 1, 1))))
            {
                outputWindow.GetPane(ref guidGeneral, out windowPane);
            }
            //if (Microsoft.VisualStudio.ErrorHandler.Failed(outputWindow.GetPane(ref guidGeneral, out windowPane)))
            //{
            //    Trace.WriteLine("Failed to get a reference to the Output Window General pane");
            //    return;
            //}

            // following instructions on MEF Output Window forum. if this doesn't work, use the commented out stuff below this
            if (windowPane != null)
            {
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowPane.Activate());
            }
            //if (Microsoft.VisualStudio.ErrorHandler.Failed(windowPane.OutputString(outputText.ToString())))
            //{
            //    Trace.WriteLine("Failed to write on the output window");
            //}
        }


    }
}
